709B - Checkpoints - CodeForces Solution


greedy implementation sortings *1500

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=1e6+100;
ll n,a,ans=1e18+100;
ll x[N];
int main()
{
	cin>>n>>a;
	for(int i=1;i<=n;i++) cin>>x[i];
	sort(x+1,x+n+1);
	if(n==1){
		cout<<0<<endl;
		return 0;
	}
	if(x[n]<=a) cout<<a-x[2]<<endl;
	else if(x[1]>=a) cout<<x[n-1]-a<<endl;
	else{
		ans=min(ans,abs(a-x[n])+abs(x[n]-x[2]));
		ans=min(ans,abs(a-x[1])+abs(x[1]-x[n-1]));
		ans=min(ans,abs(a-x[2])+abs(x[2]-x[n]));
		ans=min(ans,abs(a-x[n-1])+abs(x[1]-x[n-1]));
		cout<<ans<<endl;
	}
}
/*
6 -9
-10 -7 4 -7 0 3
*/
	   	 	 	 	 		   				  		


Comments

Submit
0 Comments
More Questions

1335A - Candies and Two Sisters
96B - Lucky Numbers (easy)
1151B - Dima and a Bad XOR
1435B - A New Technique
1633A - Div 7
268A - Games
1062B - Math
1294C - Product of Three Numbers
749A - Bachgold Problem
1486B - Eastern Exhibition
1363A - Odd Selection
131B - Opposites Attract
490C - Hacking Cypher
158B - Taxi
41C - Email address
1373D - Maximum Sum on Even Positions
1574C - Slay the Dragon
621A - Wet Shark and Odd and Even
1395A - Boboniu Likes to Color Balls
1637C - Andrew and Stones
1334B - Middle Class
260C - Balls and Boxes
1554A - Cherry
11B - Jumping Jack
716A - Crazy Computer
644A - Parliament of Berland
1657C - Bracket Sequence Deletion
1657B - XY Sequence
1009A - Game Shopping
1657A - Integer Moves